package com.lechuang.goods.manager.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lechuang.common.model.BaseEntity;
import com.lechuang.common.request.PageRequest;
import com.lechuang.common.result.PageResult;
import com.lechuang.goods.dto.GoodsListDTO;
import com.lechuang.goods.dto.GoodsSkuSaveDTO;
import com.lechuang.goods.dto.GoodsSpuSaveDTO;
import com.lechuang.goods.entity.GoodsSku;
import com.lechuang.goods.entity.GoodsSpu;
import com.lechuang.goods.manager.GoodsManager;
import com.lechuang.goods.service.GoodsSkuService;
import com.lechuang.goods.service.GoodsSpuService;
import com.lechuang.goods.vo.GoodsDetailSkuVO;
import com.lechuang.goods.vo.GoodsDetailSpuVO;
import com.lechuang.goods.vo.GoodsSpuListVO;
import com.lechuang.web.exception.BusinessException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class GoodsManagerImpl implements GoodsManager {

    @Resource
    private GoodsSpuService goodsSpuService;
    @Resource
    private GoodsSkuService goodsSkuService;

    @Override
    public PageResult<GoodsSpuListVO> pageList(PageRequest<GoodsListDTO> pageRequest) {
        Page<GoodsSpu> goodsSpuPage = goodsSpuService.pageList(pageRequest);
        List<GoodsSpu> records = goodsSpuPage.getRecords();
        List<GoodsSpuListVO> goodsListVOSpuList = BeanUtil.copyToList(records, GoodsSpuListVO.class);
        return PageResult.result(goodsListVOSpuList, goodsSpuPage, pageRequest, null);
    }

    @Override
    public void saveOrUpdate(GoodsSpuSaveDTO goodsSpuSaveDTO) {
        Long id = goodsSpuSaveDTO.getId();
        if(id == null){
            this.save(goodsSpuSaveDTO);
        }else{
            this.update(goodsSpuSaveDTO);
        }
    }

    @Override
    public void delete(Long id) {
        //删除spu
        goodsSpuService.removeById(id);
        //删除sku
        List<GoodsSku> skuList = goodsSkuService.getBySpuId(id);
        List<Long> skuIdList = skuList.stream().map(BaseEntity::getId).toList();
        goodsSkuService.removeByIds(skuIdList);
    }

    @Override
    public GoodsDetailSpuVO detail(Long id) {
        GoodsSpu goodsSpu = goodsSpuService.getById(id);
        if(goodsSpu == null){
            throw new BusinessException("要查询的商品不存在");
        }
        List<GoodsSku> skuList = goodsSkuService.getBySpuId(id);
        GoodsDetailSpuVO goodsDetailSpuVO = BeanUtil.copyProperties(goodsSpu, GoodsDetailSpuVO.class);
        List<GoodsDetailSkuVO> skuVoList = BeanUtil.copyToList(skuList, GoodsDetailSkuVO.class);
        goodsDetailSpuVO.setSkuList(skuVoList);
        return goodsDetailSpuVO;
    }

    private void update(GoodsSpuSaveDTO goodsSpuSaveDTO) {
        GoodsSpu goodsSpu = new GoodsSpu()
                .setTenantId(goodsSpuSaveDTO.getTenantId())
                .setTenantName(goodsSpuSaveDTO.getTenantName())
                .setCategoryId(goodsSpuSaveDTO.getCategoryId())
                .setName(goodsSpuSaveDTO.getName())
                .setTitle(goodsSpuSaveDTO.getTitle())
                .setFreightTemplateId(goodsSpuSaveDTO.getFreightTemplateId())
                .setAttribute(goodsSpuSaveDTO.getAttribute())
                .setGoodsNum(goodsSpuSaveDTO.getGoodsNum())
                .setGoodsStatus(goodsSpuSaveDTO.getGoodsStatus())
                .setGoodsBannerImg(goodsSpuSaveDTO.getGoodsBannerImg())
                .setGoodsDetailImg(goodsSpuSaveDTO.getGoodsDetailImg())
                .setGoodsType(goodsSpuSaveDTO.getGoodsType());
        goodsSpu.setId(goodsSpuSaveDTO.getId());
        goodsSpuService.updateById(goodsSpu);

        List<GoodsSkuSaveDTO> skuSaveDTOList = goodsSpuSaveDTO.getSkuSaveDTOList();
        //新增的sku
        List<GoodsSkuSaveDTO> insertSkuList = skuSaveDTOList.stream().filter(a -> a.getId() == null).toList();
        //修改的sku
        List<GoodsSkuSaveDTO> updateSkuList = skuSaveDTOList.stream().filter(a -> a.getId() != null).toList();
        //删除的sku
        Set<Long> updateSkuIdSet = updateSkuList.stream().map(GoodsSkuSaveDTO::getId).collect(Collectors.toSet());
        List<GoodsSku> goodsSkuList = goodsSkuService.getBySpuId(goodsSpuSaveDTO.getId());
        List<Long> deleteIdList = new ArrayList<>();
        goodsSkuList.forEach(bean -> {
            if(!updateSkuIdSet.contains(bean.getId())){
                deleteIdList.add(bean.getId());
            }
        });
        //新增
        if(CollUtil.isNotEmpty(insertSkuList)){
            List<GoodsSku> insertList = new ArrayList<>();
            insertSkuList.forEach(bean -> {
                GoodsSku goodsSku = new GoodsSku()
                        .setTenantId(goodsSpuSaveDTO.getTenantId())
                        .setSpuId(goodsSpu.getId())
                        .setCode(bean.getCode())
                        .setSpec(bean.getSpec())
                        .setInventory(bean.getInventory());
                insertList.add(goodsSku);
            });
            goodsSkuService.saveBatch(insertList);
        }
        //修改
        if(CollUtil.isNotEmpty(updateSkuList)){
            List<GoodsSku> updateList = new ArrayList<>();
            updateSkuList.forEach(bean -> {
                GoodsSku goodsSku = new GoodsSku()
                        .setTenantId(goodsSpuSaveDTO.getTenantId())
                        .setSpuId(goodsSpu.getId())
                        .setCode(bean.getCode())
                        .setSpec(bean.getSpec())
                        .setInventory(bean.getInventory());
                goodsSku.setInventory(bean.getId());
                updateList.add(goodsSku);
            });
            goodsSkuService.updateBatchById(updateList);
        }
        //删除
        if(CollUtil.isNotEmpty(deleteIdList)){
            goodsSkuService.removeBatchByIds(deleteIdList);
        }
    }

    private void save(GoodsSpuSaveDTO goodsSpuSaveDTO) {
        GoodsSpu goodsSpu = new GoodsSpu()
                .setTenantId(goodsSpuSaveDTO.getTenantId())
                .setTenantName(goodsSpuSaveDTO.getTenantName())
                .setCategoryId(goodsSpuSaveDTO.getCategoryId())
                .setName(goodsSpuSaveDTO.getName())
                .setTitle(goodsSpuSaveDTO.getTitle())
                .setFreightTemplateId(goodsSpuSaveDTO.getFreightTemplateId())
                .setAttribute(goodsSpuSaveDTO.getAttribute())
                .setGoodsNum(goodsSpuSaveDTO.getGoodsNum())
                .setGoodsStatus(goodsSpuSaveDTO.getGoodsStatus())
                .setGoodsBannerImg(goodsSpuSaveDTO.getGoodsBannerImg())
                .setGoodsDetailImg(goodsSpuSaveDTO.getGoodsDetailImg())
                .setGoodsType(goodsSpuSaveDTO.getGoodsType());
        goodsSpuService.save(goodsSpu);

        List<GoodsSku> goodsSkuList = new ArrayList<>();
        goodsSpuSaveDTO.getSkuSaveDTOList().forEach(bean -> {
            GoodsSku goodsSku = new GoodsSku()
                    .setTenantId(goodsSpuSaveDTO.getTenantId())
                    .setSpuId(goodsSpu.getId())
                    .setCode(bean.getCode())
                    .setSpec(bean.getSpec())
                    .setInventory(bean.getInventory());
            goodsSkuList.add(goodsSku);
        });
        goodsSkuService.saveBatch(goodsSkuList);
    }
}
